cmake_minimum_required(VERSION 3.13)
project(openglfunc)

set(CMAKE_CXX_STANDARD 14)

option(PLAT "platform")
option(WITH_GLUT "glut" OFF)
option(WITH_QT "qt" OFF)
option(WITH_OPENGL "opengl" OFF)
option(WITH_GLEW "glew" OFF)
option(WITH_OPENCV "opencv" OFF)
option(WITH_MEDIAVIDEO "media video" ON)
option(WITH_MEDIAVIDEO_RAW "media video" ON)

message(STATUS "plat:${PLAT}")

if (${PLAT} STREQUAL "amd64")
    message(STATUS "amd64")
    add_definitions(-DNORMAL_PC)
    if (WITH_QT)
    include_directories(/usr/include/x86_64-linux-gnu/qt5)
    link_directories(/usr/lib/x86_64-linux-gnu/)
    endif()
    set(OPENCV_PATH "/opt/data/libs/opencv-4.0.1/amd64/")
endif()

if (${PLAT} STREQUAL "tina3")
    message(STATUS "tina3")
    if (WITH_QT)
    set(QT_PATH /usr/local/qt5-arm/)
    include_directories(${QT_PATH}/include)
    include_directories(/opt/data/benew/src/cutedevice/thirdparty/nttr1/include/)
    include_directories(/opt/data/libs/ffmpeg-4.1.3/nttr1/include)
    link_directories(${QT_PATH}/lib)
    endif()
    link_directories(/opt/data/benew/src/cutedevice/thirdparty/nttr1/lib)
    set(OPENCV_PATH "/opt/data/libs/opencv-3.4.1/build_r311/install")
endif()

message(STATUS "QT path: ${QT_PATH}")

if (WITH_OPENGL)
    message(STATUS "with opengl")
endif()
if (WITH_GLUT)
    message(STATUS "with glut")
    add_definitions(-D__OPENGL_GLUT)
endif()
if (WITH_GLEW)
    message(STATUS "with glew")
    add_definitions(-D__OPENGL_GLEW)
endif()

if (WITH_QT)
    message(STATUS "with qt")
    add_definitions(-D__OPENGL_QT)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTORCC ON)

    find_package(Qt5 REQUIRED Widgets OpenGL)
endif()

if (WITH_OPENCV)
    message(STATUS "with opencv ${OPENCV_PATH}")
    add_definitions(-DWITH_OPENCV)
    include_directories(${OPENCV_PATH}/include/opencv4)
    link_directories(${OPENCV_PATH}/lib)
    set(OPENCV_FILES
            CameraCapture.cpp CameraCapture.h)
    set(OPENCV_LIBS
            opencv_imgproc
            opencv_imgcodecs
            opencv_video opencv_videoio
            opencv_core)
    if ("${PLAT}" STREQUAL "tina3")
        set(OPENCV_LIBS ${OPENCV_LIBS}
                AWIspApi isp isp_ini)
    endif()
endif()

include_directories(.)

set(BASE_FILES
        cm_print.c cm_print.h cm_logprint.h
        funcs/CMLock.cpp
        funcs/CMLock.h
        funcs/cm_utils.cpp
        funcs/cm_utils.h
        )

if (WITH_OPENGL)
    set(OPENGL_FILES
            OpenGLBase.cpp OpenGLBase.h
            )
endif()

if (WITH_OPENGL AND WITH_GLEW)
    set(OPENGL_GLEW_FILES
            glbase.h glbase.cpp)
    set(OPENGL_GLEW_LIBS glfw GLEW GL)
endif()

if (WITH_OPENGL AND WITH_GLUT)
    set(OPENGL_GLUT_FILES
            OpenGLGLUT.cpp OpenGLGLUT.h)
    set(OPENGL_GLUT_LIBS glut GLEW GL)
endif()

if (WITH_OPENGL AND WITH_QT)
    set(OPENGL_QT_FILES
            OpenGLQtSimple.cpp OpenGLQtSimple.h
            OpenGLQtWidget.cpp OpenGLQtWidget.h
            OpenGLQtEasy.cpp OpenGLQtEasy.h
            OpenGLQt.cpp OpenGLQt.h)
    if ("${PLAT}" STREQUAL "tina3")
        set(TINA3_QT_LIBS
                EGL GLESv2 Mali)
    endif()
    set(OPENGL_QT_LIBS
            Qt5::Widgets Qt5::OpenGL
            ${TINA3_QT_LIBS}
            pthread)
endif()

if (WITH_MEDIAVIDEO)
    if (WITH_MEDIAVIDEO_RAW)
        set(MEDIAVIDEO_RAW_FILES
                funcs/CMMediaOperationRaw.cpp
                funcs/CMMediaOperationRaw.h
                funcs/cm_camera.cpp
                funcs/cm_camera.h
                )
        add_definitions(-DWITH_MEDIAVIDEO_RAW -D__USE_VIN_ISP__)
        include_directories(third/nttr1/include)
    endif()
    set(MEDIAVIDEO_FILES
            funcs/CMMediaOperationBase.cpp
            funcs/CMMediaOperationBase.h
            funcs/CMMediaOperationOpenCV.cpp
            funcs/CMMediaOperationOpenCV.h
            CMMediaVideo.cpp
            CMMediaVideo.h
            ${MEDIAVIDEO_RAW_FILES}
            )
endif()

add_executable(demo
        demo.cpp
        ${BASE_FILES}
        ${OPENGL_GLEW_FILES}
        ${OPENGL_FILES}
        ${OPENGL_GLUT_FILES}
        ${OPENGL_QT_FILES}
        ${OPENCV_FILES}
        )

target_link_libraries(demo
        ${OPENCV_LIBS}
        ${OPENGL_QT_LIBS}
        ${OPENGL_GLEW_LIBS}
        ${OPENGL_GLUT_LIBS}
        ${OPENGL_LIBS})

add_executable(
        demo_fromcamera
        demo_fromcamera.cpp
        OpenGLQtRaw.cpp
        OpenGLQtRaw.h
        OpenGLQtWidget.cpp
        OpenGLQtWidget.h
        TestCameraShow.cpp
        TestCameraShow.h
        OpenGLQtEasy.cpp
        OpenGLQtEasy.h
        CameraCapture.cpp
        CameraCapture.h
        ${BASE_FILES}
        ${MEDIAVIDEO_FILES}
)

target_link_libraries(
        demo_fromcamera
        ${OPENCV_LIBS}
        ${OPENGL_QT_LIBS}
)

add_executable(
        demo_showgl
        demo_showgl.cpp
        OpenGLQtWidget.cpp
        OpenGLQtWidget.h
        cm_logprint.h
        cm_print.c
        cm_print.h
        funcs/cm_utils.cpp
        funcs/cm_utils.h
)

target_link_libraries(
        demo_showgl
        ${OPENGL_QT_LIBS}
)
